import 'dart:async';
import 'package:business_plugin/business/config_business/config_business.dart';
import 'package:get/get.dart';
import 'package:thinking_analytics/td_analytics.dart';

/// 数数sdk 初始化
class TDAnalyticsService extends GetxService {
  static TDAnalyticsService get to => Get.find();

  @override
  void onInit() {
    super.onInit();
  }

  static bool isInitialized = false;

  Future<void> initializeService() async {
    if (isInitialized) return;
    _initTD();
    isInitialized = true;
  }

  void _initTD() {
    // TrackingStatus status = await BusinessTools.getAttPermission();
    // if (status != TrackingStatus.notDetermined) {

    // }

    TDAnalytics.init(ConfigBusiness.config.tdAppId, ConfigBusiness.config.tdServerUrl);

    TDAnalytics.enableAutoTrack(TDAutoTrackEventType.APP_START |
        TDAutoTrackEventType.APP_END |
        TDAutoTrackEventType.APP_INSTALL |
        TDAutoTrackEventType.APP_CRASH);
  }

  // 获取访客id
  Future<String?> getDistinctId() async {
    String? distinctId = await TDAnalytics.getDistinctId();
    if (distinctId == null || distinctId.isEmpty) {
      return null;
    }
    return distinctId;
  }

  @override
  void onClose() {
    super.onClose();
  }
}
